/*
 * @Date: 2024-03-13 12:26:19
 * @LastEditors: bingkui.wu
 * @LastEditTime: 2024-04-02 11:20:35
 * @Description: 语音识别接口。传入音频文件，识别音频文字。
 */
import { REQUEST } from '#type/request';
import Response from '@common/Response';
import * as superagent from 'superagent';
import * as fs from "fs";
import { getToken } from '@/serives/token';
import { curTime } from '@/common/utils';

const root: REQUEST.BASIC = {
  auth: false,
  method: 'post',
  request: async (ctx: any) => {
    return new Promise(async (resolve, reject) => {
      const { files } = ctx.request.body;
      const { file } = files;
      const token = await getToken('SOUND');

      // 读取文件
      const fileData = fs.readFileSync(file.path);

      // 将文件数据转换为Base64编码
      const base64Data = fileData.toString('base64');

      const url = `https://vop.baidu.com/server_api`;
      console.log(curTime(), "调动云接口:ASR ");

      superagent.post(url).send({
        "format": "m4a",
        "rate": 16000,
        "channel": 1,
        "cuid": "WDqv3mCZvVuP27ScJVBnSLJs2tRo60LR",
        "token": token,
        "dev_pid": 1737,
        speech: base64Data,
        len: file.size
      }).end((err, res) => {
        if (err) {
          return reject(Response.Error(1001, err));
        }
        resolve(Response.Success(res.body));
        console.log(curTime(), "调动云接口:ASR 结果", res.body);

      });
    });
  }
};

export default root;